package com.itxx.keyCase.Test3;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//lock锁
public class MyThread extends Thread {
    private int ticket = 100;
        Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            try {
                lock.lock();
                if (ticket == 0) {
                    System.out.println(Thread.currentThread().getName() + ":票买完了");
                    break;
                } else if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "买:" + ticket + "张票");
                    ticket--;
                }
            } finally {
                lock.unlock();
            }

        }
    }
}
